<template>
<div class="login_container">
  <h1 align="center">登录页面</h1>
<!--  登录块-->
  <div class="login_box">
<!--    logo块-->
    <div class="avatar_box">
      <img src="../assets/logo.png" alt="" align="center">
    </div>
<!--    表单区域-->
<!--    :model="loginForm" 双向绑定-->
    <el-form ref="loginFormRef" :rules="loginRules"  :model="loginForm" label-width="80px" class="login_form">
      <el-form-item label="用户名"  prop="username">
        <el-input v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="loginForm.password"  show-password> </el-input>
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" @click="login()">提交</el-button>
        <el-button type="info" @click="resetLoginForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  data() {
    return {
      // 表单数据
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      //验证
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 12, message: '用户名长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '密码长度在 3 到 12 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods:{
    resetLoginForm(){
      // 重置表单
      this.$refs.loginFormRef.resetFields();
    },
    login(){
      this.$refs.loginFormRef.validate(valid =>{
        if(valid){// 验证成功
          // 访问后台
          axios.post('/login',this.loginForm).then(res =>{
            let flag = res.data.flag;
            console.log(res.data.flag)
            if (flag == "ok"){
              this.$message.success("操作成功!")
              window.sessionStorage.setItem("user",res.data.user);
              //跳转首页
              this.$router.push("/home"); // 页面路由跳转
             // 存储user对象
            }else {
              this.$message.error("操作失败!")

            }
          })
        }
      })
    }
  }

}

</script>

<style  scoped>
.login_form{
  padding-left: 25%;
  padding-right: 25%;
}
.btns {
  display: flex;
justify-content: center;
}
img{
  padding-left: 43%;

}
</style>